package com.shop.listener;

import com.alibaba.fastjson.JSONObject;
import com.shop.config.RabbitMqConfig;
import com.shop.domain.Order;
import com.shop.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class TakeListener {   //15天后自动收货
    @Autowired
    private OrderService orderService;
    @RabbitListener(queues = RabbitMqConfig.RECEIVING_QUEUE)
    @Transactional //事务注解
    public void receiveDead(String msg) {
        List list = JSONObject.parseObject(msg, List.class);
        for (Object o : list) {
            Order order = JSONObject.parseObject(o.toString(), Order.class);
            Order orderSelect = new Order();
            orderSelect.setId(order.getId());
            Order order1 = orderService.selectList(orderSelect).get(0);
            if(order1.getStatus() ==6){
                return;
            }
            if(order1.getStatus() != 3){
                throw new RuntimeException("订单状态异常");
            }
            order1.setStatus(6);
            order1.setUpdateTime(new Date());
            orderService.updateById(order1);
        }

    }
}
